import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';

import 'MyModel.dart';
import 'details.dart';
import 'http.dart';
import 'widgets.dart';

// https://docs.flutter.dev/get-started/fundamentals/networking#introduction-to-retrieving-data-over-the-network
// https://docs.flutter.dev/cookbook/networking/fetch-data
// https://api.flutter.dev/flutter/painting/BoxDecoration-class.html


// DetailsHomePage 看 MyModel 的最新的值
void main() {
  runApp( const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home:
      ChangeNotifierProvider(
        create: (context) => MyModel(),
        child: const MyHomePage(title: 'Flutter Demo Home Page'),
    ));
  }
}


// ChangeNotifierProvider(
// create: (context) => MyModel(),
// child: const MyHomePage(title: 'Flutter Demo Home Page'),
// ));

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Row(
        children: [
          SizedBox(height: 200),
          Consumer<MyModel>(
            builder: (context, mymodel, child) {
              return RobotText(
                text:'${mymodel.a}', // 显示计数
              );
            },
          ),
          SizedBox(width: 10),
          MyButton01(
            text: 'increment',
            containerColor: Colors.black,
            textColor: Colors.white,
            size: Size(200, 50),
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            onClick: (text) {
              Provider.of<MyModel>(context, listen: false).increment();
            },
          ),
          SizedBox(width: 10),
          MyButton01(
            text: 'decrement',
            containerColor: Colors.black,
            textColor: Colors.white,
            size: Size(200, 50),
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            onClick: (text) {
              Provider.of<MyModel>(context, listen: false).decrement();
            },
          ),
          SizedBox(width: 10),
        MyButton01(
            text: 'goto 详情页',
            containerColor: Colors.black,
            textColor: Colors.white,
            size: Size(200, 50),
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            onClick: (text) {
              Navigator.of(context).pushNamed('/a');
            },
          ),
        ],
      )
    );
  }
}
